package com.huangmb.anymark.app.fragment.user;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.alibaba.fastjson.JSON;
import com.huangmb.androidlib.utils.Logger;
import com.huangmb.androidlib.utils.PreferencesUtils;
import com.huangmb.anymark.app.R;
import com.huangmb.anymark.app.activity.UserCenterActivity;
import com.huangmb.anymark.app.api.BaseApi;
import com.huangmb.anymark.app.api.UsersApi;
import com.huangmb.anymark.app.app.AnyMarkApplication;
import com.huangmb.anymark.app.model.User;
import com.huangmb.anymark.app.model.UserContext;
import com.huangmb.anymark.app.fragment.AppBaseFragment;
import com.huangmb.common.utils.StringUtils;

/**
 * 登录页面
 *
 * @author hmb
 * @version 1.0
 * @since 2016/4/18 21:03
 */
public class LoginFragment extends AppBaseFragment {
    private static final String TAG = LoginFragment.class.getSimpleName();
    private AppCompatEditText mUserNameEditText;
    private AppCompatEditText mPasswordEditText;
    private AppCompatButton mLoginBtn;
    private AppCompatButton mRegisterBtn;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_login, container, false);
        mUserNameEditText = (AppCompatEditText) view.findViewById(R.id.et_username);
        mPasswordEditText = (AppCompatEditText) view.findViewById(R.id.et_password);
        mLoginBtn = (AppCompatButton) view.findViewById(R.id.btn_login);
        mRegisterBtn = (AppCompatButton) view.findViewById(R.id.btn_register);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getActionBar().setTitle(R.string.title_login);

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (checkUserNameAndPassword(mUserNameEditText.getText().toString(), mPasswordEditText.getText().toString())) {
                    mLoginBtn.setEnabled(true);
                } else {
                    mLoginBtn.setEnabled(false);
                }
            }
        };
        mUserNameEditText.addTextChangedListener(watcher);
        mPasswordEditText.addTextChangedListener(watcher);
        String name = PreferencesUtils.getString(getContext(),"username",null);
        String password = PreferencesUtils.getString(getContext(),"password",null);
        if(name != null && password != null){
            mUserNameEditText.setText(name);
            mPasswordEditText.setText(password);
        }

        mRegisterBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //getFragmentManager().beginTransaction().add(new RegisterFragment(),"register").commit();
                getFragmentManager().beginTransaction().replace(R.id.container,new RegisterFragment()).commit();
            }
        });

        mLoginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String userName = mUserNameEditText.getText().toString();
                final String password = mPasswordEditText.getText().toString();
                UsersApi.login(userName,password,new BaseApi.AbsListener(){
                    @Override
                    public void onSuccess(String result) {
                        Logger.d(TAG,result);
                        PreferencesUtils.put(getContext(),"username",userName);
                        PreferencesUtils.put(getContext(),"password",password);
                        User user = JSON.parseObject(result,User.class);
                        UserContext context = new UserContext();
                        context.setUser(user);
                        AnyMarkApplication.getInstance().setUserContext(context);
                        getActivity().setResult(Activity.RESULT_OK);
                        getActivity().finish();
                        //跳转到用户中心
                        //((UserCenterActivity)getActivity()).gotoUserCenterPage();
                    }

                    @Override
                    public void onFail(int errorCode, String errorMessage) {
                        super.onFail(errorCode, errorMessage);
                        toast(errorMessage);
                    }
                });

            }
        });

    }

    private boolean checkUserNameAndPassword(String name, String password) {
        if (StringUtils.isBlank(name)  || StringUtils.isBlank(password)) {
            return false;
        }
        return true;
    }
}
